/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jili.ubert.dao;

import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author ChengJiLi
 */
class H2Provider {

    private static SessionFactory factory;
    private H2Provider() {
    }
    static {
        try {
            Configuration configuration = new Configuration()
                    .setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
                    .setProperty("hibernate.connection.driver_class", "org.h2.Driver")
                    .setProperty("hibernate.connection.url", "jdbc:h2:./data/clientapi_data")
                    .setProperty("hibernate.connection.username", "jili")
                    .setProperty("hibernate.connection.password", "jili54ch")
                    .setProperty("hibernate.hbm2ddl.auto", "update")
                    .addAnnotatedClass(com.jili.ubert.dao.db.AccountBail.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.AccountFare.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.AccountIcode.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.AccountInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.AccountInvest.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.AlterOrder.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.BailTemp.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.BailTempObj.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.CancelOrder.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ClearingCurrencyRates.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ClearingDayBook.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ClearingExRight.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ClearingPrice.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ClearingProtfoliopos.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.FareTemp.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.FareTempeObj.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.FundState.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.FundTransfer.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.IOPVAccount.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.IOPVProduct.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.IOPVProductUnit.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.LogLoginInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.NewOrder.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjClassCode.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjClassCodeAtrr.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjETF.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjETFList.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjExRightInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjIndex.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjIndexList.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjNewStock.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjPriceTick.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjProtfolio.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjProtfolioList.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjStructuredFundBase.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjStructuredFundPart.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ObjUnderlyingCode.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.OrderExecuteProgress.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.PosDetail.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ProductAccount.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ProductInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.ProductUnit.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.RiskCtrlAssets.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.RiskIndex.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.RiskSets.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.SafeForbidLogin.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.SafeOtherAccess.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.TradeReportDetail.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.TradesetBroker.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.TradesetCalendar.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.TradesetExchangeLimit.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.TradesetPip.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.TradeSimOrderQuene.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.UserAccess.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.UserAccount.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.UserInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.UserPrivateInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.WorkFlowPath.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.WorkGroup.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.WorkGroupUser.class)
                    .addAnnotatedClass(com.jili.ubert.dao.db.WorkProductFlow.class)
                    .addAnnotatedClass(com.jili.ubert.dao.history.HistoryClearingCurrencyRates.class)
                    .addAnnotatedClass(com.jili.ubert.dao.history.HistoryIOPVAccount.class)
                    .addAnnotatedClass(com.jili.ubert.dao.history.HistoryIOPVProduct.class)
                    .addAnnotatedClass(com.jili.ubert.dao.history.HistoryIOPVProductUnit.class)
                    .addAnnotatedClass(com.jili.ubert.dao.history.HistoryLogLoginInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.history.HistoryObjExRightInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.history.HistoryTradeFundTransfer.class)
                    .addAnnotatedClass(com.jili.ubert.dao.history.HistoryTradesetCalendar.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentClearingAccountBail.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentClearingAccountFare.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentClearingDayBook.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentClearingExRight.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentClearingFundState.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentClearingPosDetail.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentClearingPrice.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentClearingProtfoliopos.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentObjETF.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentObjETFList.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentObjIndex.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentObjIndexList.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentTradeAlterOrder.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentTradeCancelOrder.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentTradeExecuteOrder.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentTradeNewOrder.class)
                    .addAnnotatedClass(com.jili.ubert.dao.recent.RecentTradeReportDetail.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceAccountBail.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceAccountFare.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceAccountIcode.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceAccountInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceAccountInvest.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceBailTemp.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceBailTempObj.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceFareTemp.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceFareTempeObj.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceObjInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceObjProtfolio.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceObjProtfolioList.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceObjUnderlying.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceProductAccount.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceProductInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceProductUnit.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceRiskCtrlAssets.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceRiskSets.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceSafeForbidLogin.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceSafeOtherAccess.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceTradesetExchangeLimit.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceUserAccess.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceUserAccount.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceUserInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceUserPrivateInfo.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceWorkFlowPath.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceWorkGroup.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceWorkGroupUser.class)
                    .addAnnotatedClass(com.jili.ubert.dao.trace.TraceWorkProductFlow.class);

            Properties props = configuration.getProperties();
            StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(props).build();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (HibernateException e) {
        }
    }

    public static SessionFactory getSessionFactory() {
        return factory;
    }

    public static Session getOpenSession() {
        return factory.openSession();
    }

    public static void closeSession(Session session) {
        if (session != null) {
            if (session.isOpen()) {
                session.close();
            }
        }
    }
}
